
var newer = newer || {};
newer.gd = newer.gd || {};

newer.ToolBarDiv = function(o) {
	this.domNode = o.domNode;
	
	this.init();
	this.selectedButton = {};
};

newer.ToolBarDiv.prototype = {
	init: function() {
		this.domNode.html('<div id="logo-div"><a href="/"><img id="logo" title="newcomer" alt="newcomer" src="/public/images/newcomer3.png"/></a></div><div id="tool-bar-button-area"></div>');
		
		this.domNode.removeClass("nw-event-listener").addClass("nw-event-listener");
		this.domNode.off("nw-event").on("nw-event", $.proxy(this.nweHandler,this));
		
		this.refresh();
	}, 
	
	refresh: function() {
		if(newer.gd.user.id < 0) {
			$("#tool-bar-button-area").html("");
			this.addSignInInput();
			this.addSignInButton();
			this.addSignUpButton();
		} else {
			$("#tool-bar-button-area").html("");
			this.addHomepageButton();
			this.addProfileButton();
			this.addCirclesButton();
			this.addWelcomeMessage();
			this.addSignOutButton();
			this.addBackgroundButton();
			this.updateSelectedButton();
			this.runSelectedButtonEffect(true);
		}
	},
	
	nweHandler: function(o,c) {
		if (c.type == newer.EventHelper.AUTH_TYPE) {
			this.refresh();
		} else if(c.type == newer.EventHelper.PAGE_TYPE && newer.gd.user.id >= 0) {
			this.updateSelectedButton();
			this.runSelectedButtonEffect(false);
		}
	},
	
	addSignUpButton: function() {
		$("#tool-bar-button-area").append('<div id="tool-bar-signup-button" class="tool-bar-button-area-pos"></div>');
		new newer.ToolBarButton({
			domNode:$("#tool-bar-signup-button"),
			title:"Join us",
			clickHandler: function(e) {
				e.preventDefault();
			}
		});
	}, 
	
	addSignInButton: function() {
		$("#tool-bar-button-area").append('<div id="tool-bar-login-button" class="tool-bar-button-area-pos"></div>');
		new newer.ToolBarButton({
			domNode:$("#tool-bar-login-button"),
			title:"Sign in",
			clickHandler: $.proxy(function(e) {
				e.preventDefault();
				this.loginSubmit();
			},this)
		});
	},
	
	addSignInInput: function() {
		$("#tool-bar-button-area").append('<div id="tool-bar-login-input"></div>');
		$("#tool-bar-login-input")
		.html('Email <input type="text" id="tool-bar-login-email"/> Password <input type="password" id="tool-bar-login-password"/>')
		.addClass("tool-bar-button-area-pos");
		$("#tool-bar-login-password").on("keypress", $.proxy(function(event) {
			if(event.keyCode == 13) {
				this.loginSubmit();
			}
		},this));
	},
	
	addSignOutButton: function() {
		$("#tool-bar-button-area").append('<div id="tool-bar-signout-button" class="tool-bar-button-area-pos"></div>');
		new newer.ToolBarButton({
			domNode:$("#tool-bar-signout-button"),
			title:"Sign out",
			clickHandler: $.proxy(function(e) {
				e.preventDefault();
				$.post("/Login/logoutJSON", {}, function() {
					newer.UriHelper.getGlobals(true);
					$(".nw-event-listener").trigger("nw-event", {
						type:newer.EventHelper.AUTH_TYPE
					});
				});
			},this)
		});
	},
	
	addHomepageButton: function() {
		$("#tool-bar-button-area").append('<div id="tool-bar-home-button" class="tool-bar-button-area-pos"></div>');
		this.homeButton = new newer.ToolBarButton({
			domNode:$("#tool-bar-home-button"),
			title:"Home",
			clickHandler: $.proxy(function(e) {
				e.preventDefault();
				var toStatus = {ptype:newer.UriHelper.HOME_TYPE};
				var fromStatus = {ptype:newer.gd.pstatus.ptype};
				newer.UriHelper.initRedirection({
					fromStatus:fromStatus,
					toStatus:toStatus
				});
				$(".nw-event-listener").trigger('nw-event',{
					type:newer.EventHelper.PAGE_TYPE,
					fromStatus:fromStatus,
					toStatus:toStatus
				});
			},this)
			// mouseenterHandler: $.proxy(this.btnMouseenterHandler,this),
			// mouseleaveHandler: $.proxy(this.btnMouseleaveHandler,this)
		});
		
	},
	
	addProfileButton: function() {
		$("#tool-bar-button-area").append('<div id="tool-bar-profile-button" class="tool-bar-button-area-pos"></div>');
		this.profileButton = new newer.ToolBarButton({
			domNode:$("#tool-bar-profile-button"),
			title:"Profile",
			clickHandler: $.proxy(function(e) {
				e.preventDefault();
				var toStatus = {
					ptype:newer.UriHelper.PEOPLE_TYPE,
					id:newer.gd.user.id
				};
				var fromStatus = {ptype:newer.gd.pstatus.ptype};
				newer.UriHelper.initRedirection({
					fromStatus:fromStatus,
					toStatus:toStatus
				});
				$(".nw-event-listener").trigger('nw-event',{
					type:newer.EventHelper.PAGE_TYPE,
					fromStatus:fromStatus,
					toStatus:toStatus
				});
			},this)
			// mouseenterHandler: $.proxy(this.btnMouseenterHandler,this),
			// mouseleaveHandler: $.proxy(this.btnMouseleaveHandler,this)
		});
	},
	
	addCirclesButton: function() {
		$("#tool-bar-button-area").append('<div id="tool-bar-circles-button" class="tool-bar-button-area-pos" style="margin-right:50px;"></div>');
		this.circlesButton = new newer.ToolBarButton({
			domNode:$("#tool-bar-circles-button"),
			title:"Circles",
			clickHandler: $.proxy(function(e) {
				e.preventDefault();
				var toStatus = {
					ptype:newer.UriHelper.CIRCLE_TYPE,
					id:"1"
				};
				var fromStatus = {ptype:newer.gd.pstatus.ptype};
				newer.UriHelper.initRedirection({
					fromStatus:fromStatus,
					toStatus:toStatus
				});
				$(".nw-event-listener").trigger('nw-event',{
					type:newer.EventHelper.PAGE_TYPE,
					fromStatus:fromStatus,
					toStatus:toStatus
				});
			},this)
			// mouseenterHandler: $.proxy(this.btnMouseenterHandler,this),
			// mouseleaveHandler: $.proxy(this.btnMouseleaveHandler,this)
		});
	},
	
	addBackgroundButton: function() {
		// $("#tool-bar-button-area").append('<div id="tool-bar-bg-button" class="tool-bar-button-area-pos" style="position:absolute;background-color:#9ab;z-index:-100;box-shadow:inset 0px 0px 4px 2px rgba(0,0,0,0.5)"></div>');
		$("#tool-bar-button-area").append('<div id="tool-bar-bg-button" class="tool-bar-button-area-pos" style="position:absolute;"></div>');
		this.bgButtonNode = $("#tool-bar-bg-button");
		this.bgButtonNode.css('left','900px');
		this.bgButtonNode.css('width','200px');
		this.bgButtonNode.css('opacity','0.5');

	},
	
	updateSelectedButton: function() {
		if (newer.gd.pstatus.ptype == newer.UriHelper.PEOPLE_TYPE) {
			this.selectedButton = this.profileButton;
		} else if (newer.gd.pstatus.ptype == newer.UriHelper.HOME_TYPE) {
			this.selectedButton = this.homeButton;
		} else if (newer.gd.pstatus.ptype == newer.UriHelper.CIRCLE_TYPE) {
			this.selectedButton = this.circlesButton;
		} else {
			this.selectedButton = this.homeButton;
		}
	},
	
	runSelectedButtonEffect: function(init) {
		this.originalCss = {
			left:this.selectedButton.domNode.position().left,
			width:this.selectedButton.domNode.css('width')
		}
		if (init == true) {
			this.bgButtonNode.css(this.originalCss);
		} else {
			this.bgButtonNode.stop(true,true).animate(this.originalCss);
		}
	},
	
	btnMouseenterHandler: function(e) {
		this.bgButtonNode.stop(true,true).animate({
			left:$(e.target).position().left,
			width:$(e.target).css('width')
		});
	},
	
	btnMouseleaveHandler: function(e) {
		this.bgButtonNode.stop(true,true).animate(this.originalCss);
	},
	
	
	loginSubmit: function() {
		var self = this;
		$.debug("get clicked!");
		$.post("/Login/connectJSON", {u:$("#tool-bar-login-email").val(), p:$("#tool-bar-login-password").val()}, function(result) {
			if(result == false) {

			} else {
				newer.gd.user = result;
				$(".nw-event-listener").trigger("nw-event", {
					type:newer.EventHelper.AUTH_TYPE
				});
			}
		}, "json");
	},
	
	addWelcomeMessage: function() {
		$("#tool-bar-button-area").append('<div id="tool-bar-welcome-div" style="margin-right:50px;"></div>');
		$("#tool-bar-welcome-div").html("Welcome, "+newer.gd.user.name+"!").addClass("tool-bar-button-area-pos");
	}
};
	